package top.jybill.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import top.jybill.domain.MyPageInfo;
import top.jybill.domain.Product;
import top.jybill.domain.ResponseData;
import top.jybill.exception.MyException;
import top.jybill.service.IProductService;
import top.jybill.service.Impl.ProductServiceImpl;
import top.jybill.utils.CharCodeUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 商品控制层
 */
@Controller
@RequestMapping("/product")
public class ProductController {

  @Autowired
  protected ResponseData res;
  @Autowired
  private IProductService productService;

  /**
   * 查询所有产品
   * @return
   */
  @RequestMapping("/findAll")
  public @ResponseBody ResponseData findAll() {
    res.clean();
    List<Product> products = productService.findAll();
    if (products == null)  {
      res.metaPut("status", 500);
      res.metaPut("msg", "请联系管理员");
      return res;
    }
    res.metaPut("status", 200);
    res.metaPut("msg", "成功");
    res.dataPut("products", products);
    return res;
  }

  /**
   * 添加商品
   * @param product
   * @return
   */
  @RequestMapping("/saveProduct")
  @ResponseBody
  public ResponseData saveProduct(@RequestBody Product product) {
    res.clean();
    try {
      productService.saveProduct(product);
    } catch (Exception e) {
      e.printStackTrace();
      if (e instanceof MyException) {
        res.metaPut("status", 400);
        res.metaPut("msg", "信息不完整");
        return res;
      }
      res.metaPut("status", 400);
      res.metaPut("msg", "添加失败请联系管理员");
      return res;
    }
    res.metaPut("status", 200);
    res.metaPut("msg", "添加成功");
    return res;
  }

  /**
   * 分页查询商品
   * @param page
   * @param request
   * @return
   * @throws Exception
   */
  @RequestMapping(value = "/findProductsByPage", method = RequestMethod.GET)
  @ResponseBody
  public ResponseData findProductsByPage(MyPageInfo page,
                     HttpServletRequest request) throws Exception {
    // get参数转码UTF-8
    if (page.getQuery() != null || page.getQuery().length() > 0) {
      page.setQuery(CharCodeUtil.getUTF8CharCode(page.getQuery(), "UTF-8"));
    }
    res.clean();
    MyPageInfo retPage = productService.findProductByPage(page);
    res.dataPut("page", retPage);
    res.metaPut("status", 200);
    res.metaPut("msg", "OK");
    return res;
  }


  /**
   * 删除产品
   * @param pid
   * @return
   */
  @RequestMapping(value = "/delProduct/{pid}", method = RequestMethod.DELETE)
  @ResponseBody
  public ResponseData delProduct(@PathVariable("pid") String pid) {
    res.clean();
    productService.delProduct(pid);
    res.metaPut("status", 200);
    res.metaPut("msg", "OK");
    return res;
  }

  /**
   * 更新产品
   * @param product
   * @return
   */
  @RequestMapping(value = "/updateProduct", method = RequestMethod.POST)
  @ResponseBody
  public ResponseData updateProduct(@RequestBody Product product) {
    res.clean();
    productService.updateProduct(product);
    res.metaPut("status", 200);
    res.metaPut("msg", "OK");
    return res;
  }

  /**
   * 根据商品id查商品
   * @param pid
   * @return
   */
  @RequestMapping(value = "/findProductByPid", method = RequestMethod.GET)
  @ResponseBody
  public ResponseData findProductByPid(String pid) {
    res.clean();
    res.dataPut("product", productService.findProductByPid(pid));
    res.metaPut("status", 200);
    res.metaPut("msg", "OK");
    return res;
  }
}
